package cn.pink.core.support.function;

/**
 * @Author: pink
 * @Date: 2022/6/29 21:26
 */
public class CommonFunction {
    /** 回掉函数参数数量 */
    private int paramCount = 0;
    /** 回掉函数 */
    private Object function;

    public CommonFunction(Object function, int paramCount) {
        this.paramCount = paramCount;
        this.function = function;
    }

    public int getParamCount() {
        return this.paramCount;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void apply(Object ... m) {
        assert(m.length == this.paramCount);
        Object f = this.function;
        switch (m.length) {
            case 0: ((IronFunction0) f).apply(); break;
            case 1: ((IronFunction1) f).apply(m[0]); break;
            case 2: ((IronFunction2) f).apply(m[0], m[1]); break;
            case 3: ((IronFunction3) f).apply(m[0], m[1], m[2]); break;
            case 4: ((IronFunction4) f).apply(m[0], m[1], m[2], m[3]); break;
            case 5: ((IronFunction5) f).apply(m[0], m[1], m[2], m[3], m[4]); break;
            case 6: ((IronFunction6) f).apply(m[0], m[1], m[2], m[3], m[4], m[5]); break;
            case 7: ((IronFunction7) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6]); break;
            case 8: ((IronFunction8) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7]); break;
            case 9: ((IronFunction9) f).apply(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); break;
            default: break;
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T getFunc() {
        return (T) function;
    }
}
